1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
| <?php
add_action( 'widgets_init', 'example_load_widgets' );
function example_load_widgets() { register_widget( 'Example_Widget' ); }
class Example_Widget extends WP_Widget {
function Example_Widget() { $widget_ops = array( 'classname' => 'example', 'description' => __('An example widget that displays a person\'s name and sex.', 'example') );
$control_ops = array( 'width' => 300, 'height' => 350, 'id_base' => 'example-widget' );
$this->WP_Widget( 'example-widget', __('Example Widget', 'example'), $widget_ops, $control_ops ); }
function widget( $args, $instance ) { extract( $args );
$title = apply_filters('widget_title', $instance['title'] ); $name = $instance['name']; $sex = $instance['sex']; $show_sex = isset( $instance['show_sex'] ) ? $instance['show_sex'] : false;
echo $before_widget;
if ( $title ) echo $before_title . $title . $after_title;
if ( $name ) printf( '<p>' . __('Hello. My name is %1$s.', 'example') . '</p>', $name );
if ( $show_sex ) printf( '<p>' . __('I am a %1$s.', 'example.') . '</p>', $sex );
echo $after_widget; }
function update( $new_instance, $old_instance ) { $instance = $old_instance;
$instance['title'] = strip_tags( $new_instance['title'] ); $instance['name'] = strip_tags( $new_instance['name'] );
$instance['sex'] = $new_instance['sex']; $instance['show_sex'] = $new_instance['show_sex'];
return $instance; }
function form( $instance ) {
$defaults = array( 'title' => __('Example', 'example'), 'name' => __('John Doe', 'example'), 'sex' => 'male', 'show_sex' => true ); $instance = wp_parse_args( (array) $instance, $defaults ); ?>
<!-- Widget Title: Text Input --> <p> <label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e('Title:', 'hybrid'); ?></label> <input id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" value="<?php echo $instance['title']; ?>" style="width:100%;" /> </p>
<!-- Your Name: Text Input --> <p> <label for="<?php echo $this->get_field_id( 'name' ); ?>"><?php _e('Your Name:', 'example'); ?></label> <input id="<?php echo $this->get_field_id( 'name' ); ?>" name="<?php echo $this->get_field_name( 'name' ); ?>" value="<?php echo $instance['name']; ?>" style="width:100%;" /> </p>
<!-- Sex: Select Box --> <p> <label for="<?php echo $this->get_field_id( 'sex' ); ?>"><?php _e('Sex:', 'example'); ?></label> <select id="<?php echo $this->get_field_id( 'sex' ); ?>" name="<?php echo $this->get_field_name( 'sex' ); ?>" class="widefat" style="width:100%;"> <option <?php if ( 'male' == $instance['format'] ) echo 'selected="selected"'; ?>>male</option> <option <?php if ( 'female' == $instance['format'] ) echo 'selected="selected"'; ?>>female</option> </select> </p>
<!-- Show Sex? Checkbox --> <p> <input class="checkbox" type="checkbox" <?php checked( $instance['show_sex'], true ); ?> id="<?php echo $this->get_field_id( 'show_sex' ); ?>" name="<?php echo $this->get_field_name( 'show_sex' ); ?>" /> <label for="<?php echo $this->get_field_id( 'show_sex' ); ?>"><?php _e('Display sex publicly?', 'example'); ?></label> </p>
<?php } }
?>
|